package cn.gzsxt.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.gzsxt.po.PageResult_name;
import cn.gzsxt.po.T_sysUser;
import cn.gzsxt.po.T_task;
import cn.gzsxt.query.PageQueryObject;
import cn.gzsxt.service.TaskAddService;
import cn.gzsxt.service.impl.TaskAddServiceImpl;
import cn.gzsxt.vo.MsgObj;

@WebServlet("/taskAdd.sxt")
public class TaskAddController extends HttpServlet {

	/**
	 * 
	 */

	private TaskAddService service = new TaskAddServiceImpl();

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String cmd = req.getParameter("cmd");
		if ("checkTask".equals(cmd)) {
			checkTask(req, resp);
		} else if ("deltask".equals(cmd)) {
			deltask(req, resp);
		} else if ("updateTask".equals(cmd)) {
			updateTask(req, resp);
		} else if ("checkId_Name".equals(cmd)) {
			checkId_Name(req, resp);
		} else if ("delpagetask".equals(cmd)) {
			delPageTask(req, resp);
		}

	}

	private void delPageTask(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf8");
		String id = req.getParameter("id");
		String[] ids = id.split(",");
		MsgObj msgObj = new MsgObj(0, "删除失败");
		if (id != null && !"".equals(id.trim())) {
			Integer state = service.delpagetask(ids);
			if (state == ids.length) {
				msgObj = new MsgObj(1, "删除成功");
			}
		}
		resp.getWriter().print(JSON.toJSONString(msgObj));

	}

	private void checkId_Name(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf8");
		List<T_sysUser> list = service.checkId_Name();
		resp.getWriter().print(JSON.toJSONString(list));
	}

	private void updateTask(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		// 插入和修改一起
		T_task task = getParameter2Obj(req);
		int row = 0;
		if (task.getId() == null) {
			System.out.println(task);
			row = service.updateTask(task);
		} else {
			Integer id = task.getId();
			row = service.updateTask(task, id);
		}

		MsgObj msgObj = null;
		if (row == 1) {// 修改成功
			msgObj = new MsgObj(1, "修改成功");
		} else {
			msgObj = new MsgObj(0, "修改失败");
		}
		resp.getWriter().print(JSON.toJSONString(msgObj));
	}

	private void deltask(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		String id = req.getParameter("id");
		MsgObj msgObj = new MsgObj(0, "删除失败");
		if (id != null && !"".equals(id.trim())) {
			Integer state = service.deltask(id);
			if (state == 1) {
				msgObj = new MsgObj(1, "删除成功");
			}
		}
		resp.getWriter().print(JSON.toJSONString(msgObj));
	}

	private void checkTask(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
		String keyword = req.getParameter("keyword");
		PageQueryObject qo = new PageQueryObject();
		if (currentPage != null) {
			qo.setCurrentPage(Integer.valueOf(currentPage));
		}
		if (pageSize != null) {
			qo.setPageSize(Integer.valueOf(pageSize));
		}
		if (keyword != null && !"".equals(keyword.trim())) {

			qo.setKeyword(keyword);
		}
		PageResult_name pageResult = service.task(qo);
		req.setAttribute("pageResult", pageResult);
		req.setAttribute("keyword", qo.getKeyword());
		req.getRequestDispatcher("/WEB-INF/task/taskadd.jsp").forward(req, resp);
	}

	public T_task getParameter2Obj(HttpServletRequest req) {
		String annex = req.getParameter("annex");
		String taskresult = req.getParameter("taskresult");
		String urgent = req.getParameter("urgent");
		String taskdetails = req.getParameter("taskdetails");
		String status = req.getParameter("status");
		String person = req.getParameter("person");
		String title = req.getParameter("title");
		String fintime = req.getParameter("fintime");
		String creattime = req.getParameter("creattime");
		String id = req.getParameter("id");

		T_task task = new T_task();
		task.setTitle(title);
		task.setAnnex(annex);
		task.setTaskresult(taskresult);
		task.setContent(taskdetails);
		if (urgent != null && !"".equals(urgent.trim())) {
			task.setUrgent(Integer.valueOf(urgent));
		}
		if (status != null && !"".equals(status.trim())) {
			task.setStatus(Integer.valueOf(status));
		}
		if (person != null && !"".equals(person.trim())) {
			task.setPerson(Integer.valueOf(person));
		}

		if (id != null && !"".equals(id.trim())) {
			task.setId(Integer.valueOf(id));
		}
		try { // 格式化日期
			Date date = null;
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

			if (creattime != null && !"".equals(creattime.trim())) {
				date = format.parse(creattime);
				task.setCreattime(date);
			}
			if (fintime != null && !"".equals(fintime.trim())) {
				date = format.parse(fintime);
				task.setFintime(date);
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}

		return task;
	}
}
